package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.Userlock;
import com.itheima.mapper.UserlockMapper;
import com.itheima.service.db.UserlockService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserlockServiceImpl implements UserlockService {
    @Autowired
    UserlockMapper userlockMapper;

    @Override
    public Userlock findByUserAndNaire(Long userId, Long naireId) {
        LambdaQueryWrapper<Userlock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Userlock::getUserId, userId).eq(Userlock::getQuestionnaireId, naireId);
        return userlockMapper.selectOne(lqw);
    }

    @Override
    public void lockDown(Long userId, Long naireId) {
        LambdaQueryWrapper<Userlock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Userlock::getUserId, userId).eq(Userlock::getQuestionnaireId, naireId + 1);
        Userlock userlock = userlockMapper.selectOne(lqw);
        if (userlock != null) {
            if (userlock.getIsLock() == 1) {
                userlock.setIsLock(0);
                userlockMapper.updateById(userlock);
            }
        }
    }

    @Override
    public void newUser(Long id) {
        userlockMapper.insert(new Userlock(id, 1L, 0));
        userlockMapper.insert(new Userlock(id, 2L, 1));
        userlockMapper.insert(new Userlock(id, 3L, 1));
    }
}